<?php

namespace app\admin\controller;

use app\admin\validate\LoginValidate;
use app\BaseController;
use app\admin\model\SystemAdmin;
use app\Request;
use app\services\{RedisService, CaptchaService};


class PublicController extends BaseController
{
    private $ip;
    public function __construct(Request $request)
    {
        $headers = $request->header();
        $this->ip = $headers['client-id'];
    }

    public function captcha() 
    {
        $this->success('success', CaptchaService::generateCaptcha($this->ip), 200);
    } 

    public function login(Request $request)
    {
        $validate = new LoginValidate();
        $validate->checkParams();
        $data = $validate->getDataByRule($request->post());
        $this->checkCaptcha($data['captcha']);
        
        $this->success('success', SystemAdmin::checkLogin($data), 200);
    }

    private function checkCaptcha($code)
    {
        CaptchaService::verification($code, $this->ip);
    }

    public function loginOut()
    {
        $this->success('success', SystemAdmin::loginOut(), 201);
    }
}